package com.hualude.roadsign.repository;


import com.hualude.roadsign.dto.ActorDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.UserDto;
import com.hualude.roadsign.dto.UserOrgDto4Notice;
import com.hualude.roadsign.model.UserEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface UserRepository extends CrudRepository<UserEntity, Long> {

    /**
     * @Description: 用户登录验证与返回信息封装
     * @Author: feng
     * @Date: 2017/2/20 23:45
     */
    public UserDto loginCheck(String loginName, String loginPwd);

    /**
     * @Description: 根据Id获取教师信息
     * @Author: SN
     * @Date: 2017/2/20 23:45
     */
    @Query(value = "select new com.hualude.roadsign.dto.UserDto(t.userId,t.loginName,t.userName,t.userSex,t.userMobile," +
            "t.userEmail,t.userIcon,t.userLoginTime,t.userState,t.userRemarks) from UserEntity t where t.userId = ?1")
    public UserDto getUserById(Integer userId);

    /**
     * @Description: 根据Id更新教师部分信息
     * @Author: SN
     * @Date: 2017/2/20 23:45
     */
    @Modifying
    @Query(value = "update UserEntity t set t.userName = ?1,t.userSex = ?2,t.userMobile = ?3,t.userEmail = ?4,t.userIcon = ?5,t.userRemarks = ?6 where t.userId = ?7")
    public Integer updateUserById(String userName, String userSex, String userMobile, String userEmail, String userIcon, String userRemarks, Integer userId);

    @Modifying
    @Query(value = "update UserEntity t set t.loginPwd = ?1 where t.userId = ?2")
    public Integer updatePwdById(String newPwd, Integer userId);

    @Query(value = "select t.loginPwd from UserEntity t where t.userId = ?1")
    public String getPwdById(Integer userId);

    @Query(value = "select new com.hualude.roadsign.dto.UserDto(t.userId,t.loginName,t.userName,t.userSex,t.userMobile,t.userEmail,t.userIcon,t.userLoginTime,t.userState,t.userRemarks) from UserEntity t ")
    public List<UserDto> getUser4addNotice();

    @Query(value = "select u.orgId from UserEntity u where u.userId = ?1")
    public Integer getUserOrg4Notice(Integer userId);

    @Query(value = "select new com.hualude.roadsign.dto.UserOrgDto4Notice(u.orgId,u.userId,u.userName) from UserEntity u where u.userId = ?1")
    public List<UserOrgDto4Notice> getUserInfo2NoticeByUserId(Integer userId);

    /**
     * @Description: 根据登录名获取手机号
     * @Author feng
     * @Date 2017/3/10 11:52
     */
    @Query(value = "select t.userMobile from UserEntity t where t.loginName = ?1")
    public String getPhoneByLoginName(String loginName);

    /**
     * @Description: 根据登录名获取用户名
     * @Author feng
     * @Date 2017/3/10 11:53
     */
    @Query(value = "select t.userName from UserEntity t where t.loginName = ?1")
    public String getUserNameByLoginName(String loginName);

    /**
     * @Description: 根据登录名存验证码到UserRemakers中
     * @Author feng
     * @Date 2017/3/10 11:53
     */
    @Modifying
    @Query(value = "update UserEntity t set t.userRemarks = ?2 where t.loginName = ?1")
    public Integer updateUserRemakersByLoginName(String loginName, String code);

    /**
     * @Description: 根据登录名获取验证码
     * @Author feng
     * @Date 2017/3/10 11:53
     */
    @Query(value = "select t.userRemarks from UserEntity t where t.loginName = ?1")
    public String getUserRemakersByLoginName(String loginName);

    /**
     * @Description: 根据登录名获取密码
     * @Author feng
     * @Date 2017/3/11 11:55
     */
    @Query(value = "select t.loginPwd from UserEntity t where t.loginName = ?1")
    public String getPwdByLoginName(String loginName);

    /**
     * @Description: 根据登录名重置密码
     * @Author feng
     * @Date 2017/3/11 11:58
     */
    @Modifying
    @Query(value = "update UserEntity t set t.loginPwd = ?1 where t.loginName = ?2")
    public Integer updatePwdByLoginName(String newPwd, String loginName);

    /**
     * @Description: 根据用户Id获取用户身份
     * @Author feng
     * @Date 2017/3/11 11:44
     */
    @Query(value = "select a.actorName from UserEntity  u, UserActorEntity ua, ActorEntity a where u.userId=ua.userId and ua.actorId=a.actorId and u.userId = ?1")
    public List<String> getActorNameByUserId(Integer userId) throws Exception;

    /**
     * @Description: 获取用户姓名和Id
     * @Author: xu-jiaHui
     * @Date: 2017/3/18 16:41
     */
    @Query(value = "select new com.hualude.roadsign.dto.UserDto(u.userId,u.userName,u.orgId)  from UserEntity u ")
    public List<UserDto> getUserNameIdAndOrgId() throws Exception;

    /**
     * @Description: 根据角色获取UserNameId和orgId
     * @Author: xu-jiaHui
     * @Date: 2017/3/20 20:53
     */
    public List<UserOrgDto4Notice> getUserNameIdAndOrgIdByActorId(Integer ActorId) throws Exception;

    /**
     * @Description: 根据用户Id获取名字
     * @Author: shen-xiaoyong
     * @Date: 2017/3/21 下午8:26
     */
    @Query(value = "select userName from UserEntity  where userId = ?1")
    public String getUserNameByUserId(Integer userId) throws Exception;

    @Modifying
    @Query(value = "update UserEntity t set t.userIcon = ?2, t.userName = ?3,t.userSex = ?4,t.userEmail = ?5 where t.userId = ?1")
    public Integer updateUserById4app(Integer userId, String userIcon, String userName, String userSex, String userEmail);


    /**
     * @Description: 获取用户信息用于DT
     * @Author: xu-jiaHui
     * @Date: 2017/4/7 18:40
     */
    public List<UserDto> getUser4DT(DTRequestDto dtRequestDto, Integer orgId, Integer actorId) throws Exception;


    public Integer getCount4DT(Integer orgId, String strSearch, Integer actorId) throws Exception;

    /**
     * @Description: 停用账户
     * @Author: xu-jiaHui
     * @Date: 2017/4/20 19:29
     */
    @Modifying
    @Query(value = "update UserEntity set userState=1 where userId = ?1")
    public Integer deactivateUserById(Integer userId) throws Exception;

    /**
     * @Description: 启用账户
     * @Author: xu-jiaHui
     * @Date: 2017/4/25 23:00
     */
    @Modifying
    @Query(value = "update UserEntity set userState=0 where userId = ?1")
    public Integer activateUserById(Integer userId) throws Exception;

    @Modifying
    @Query(value = "update UserEntity t set t.userName=?1 ,t.userMobile=?2,t.userSex=?3,t.userEmail=?4,t.userState=?5,t.userRemarks=?6,t.orgId=?8 where t.userId=?7")
    public Integer updateUserByUserId(String userName, String userMobile, String userSex, String userEmail, Integer userState, String userRemarks, Integer userId, Integer orgId) throws Exception;

    /**
     * @Description: 通过loginName 返回条数
     * @Author: xu-jiaHui
     * @Date: 2017/4/10 19:47
     */
    public Integer getCountByLoginName(String loginName) throws Exception;

    /**
     * @Description: 获取发送用户2NOTICE
     * @Author: Panjianan
     * @Data: 17:36:2017/4/9
     * @params:
     **/
    @Query(value = "select userId from UserEntity  where orgId = ?1")
    public List<Integer> getUserIds2NoticeAdd(Integer orgId) throws Exception;

    /**
     * @Description: 删除用户
     * @Author: xu-jiaHui
     * @Date: 2017/5/1 0:01
     */
    @Modifying
    @Query(value = "delete from UserEntity where userId=?1")
    public Integer deleteUserById(Integer userId) throws Exception;

    /**
     * @Description: 获取所有用户
     * @Author: pan-jianan
     * @Date: 2017/4/21 14:06
     */
    @Query(value = "select userId from UserEntity")
    public List<Integer> getUserIds2NoticeApp() throws Exception;

    /**
     * @Description: 获取日志信息DT
     * @Author: shao-yikun
     * @Date: 2017/5/1 19:32
     */
    public List<UserDto> getLog4DT(DTRequestDto dtRequestDto) throws Exception;

    /**
     * @Description: 获取日志信息DT数量
     * @Author: shao-yikun
     * @Date: 2017/5/1 19:37
     */
    public Integer getLogCount4DT(String searchText) throws Exception;

    /**
     * @Description: 更新IP
     * @Author: shao-yikun
     * @Date: 2017/5/1 21:37
     */
    @Modifying
    @Query(value = "update UserEntity set ip = ?2 where loginName = ?1")
    public void updateIP(String loginName, String ip) throws Exception;

    /**
     * @Description: 重置密码666666
     * @Author: xu-jiaHui
     * @Date: 2017/5/1 21:41
     */
    @Modifying
    @Query(value = "update UserEntity set loginPwd='666666' where userId=?1")
    public Integer resetPswByUserId(Integer userId) throws Exception;

    /**
     * @Description: 获取所有用户的actorName
     * @Author: xu-jiaHui
     * @Date: 2017/5/6 14:02
     */
    public List<ActorDto> getActorNames4User() throws Exception;


    /**
     * @Description: 根据公司和用户权限返回用户（安卓）
     * @Author: shao-yikun
     * @Date: 2017/5/20 23:50
     */
    @Query(value = "select new com.hualude.roadsign.dto.UserDto(u.userId, u.userName) " +
            "from UserEntity u, UserActorEntity a " +
            "where u.userId = a.userId and orgId = ?1 and actorId = ?2")
    public List<UserDto> getUserIdsByOrgIdAndActorId(Integer orgId, Integer actorId) throws Exception;

    /**
     * @Description: 获取维修人员
     * @Author: jiang-weirong
     * @Date: 2017/7/12 21:24
     */
    @Query(value = "select new com.hualude.roadsign.dto.UserDto(u.userId, u.userName) " +
            "from UserEntity u,UserActorEntity ua " +
            "where u.userId = ua.userId and u.orgId = ?1 and ua.actorId = 5")
    public List<UserDto> getSolarMaintainer(Integer solarOrgId) throws Exception;
}

